﻿using MoetutuTranslate.Helper;
using MoetutuTranslate.ScannerHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ListView;

namespace MoetutuTranslate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitView();
        }

        private void InitView()
        {
            comboBox1.SelectedIndex = 0;
        }
        bool UiLock = true;
        private void button1_Click(object sender, EventArgs e)
        {
             RunThreadAsync();

        }

        private async Task RunThreadAsync()
        {
            listView1.Items.Clear();
         
            List<String> list = await Sacnner.GetListAsync(textBox1.Text);

            AddToListView(listView1,list);

            textBox1.Text = "";
        }

      

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            PrintLog.Log("List1选择了{0}个", listView1.SelectedItems.Count);


         
            if (listView1.SelectedItems.Count <= 0) return;
            else
            {
                if (!ViewHelper.TimerBlock())
                {
                    Console.WriteLine("距离上次间隔过短，程序锁死");
                    return;
                }
              
                listView1.BeginUpdate();
                Console.WriteLine("List1选择了位置{0}，总{1}个选项，时间{2}", listView1.SelectedItems[0].Index,listView1.Items.Count, DateTime.Now.ToString());
                string Tag = listView1.SelectedItems[0].Text;
                textBox2.Text = Tag;
                textBox4.Text = Tag;
                listView2.Items.Add(Tag);
                ListRemove(listView1,listView1.SelectedItems[0].Index);
                TranslateHelper.OpenWebView.GetLink(comboBox1.Text, Tag);
                listView1.EndUpdate();
            }
        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            PrintLog.Log("List2选择了{0}个", listView2.SelectedItems.Count);

            if (listView2.SelectedItems.Count <= 0) return;
            else
            {
                listView2.BeginUpdate();
                PrintLog.Log("List2选择了位置{0}，总{1}个选项，时间{2}", listView2.SelectedItems[0].Index,listView2.Items.Count,DateTime.Now.ToString());
                string Tag = listView2.SelectedItems[0].Text;
                listView1.Items.Add(ListViewHelper.ListView(new List<string>() { Tag, "未知" }));
                ListRemove(listView2, listView2.SelectedItems[0].Index);
                listView2.EndUpdate();

            }
        }
        /// <summary>
        /// 删除一项
        /// </summary>
        /// <param name="listViewTemp"></param>
        /// <param name="index"></param>
        private void ListRemove(ListView listViewTemp, int index)
        {
            try
            {
                listViewTemp.Items.RemoveAt(listViewTemp.SelectedItems[0].Index);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            List<string> WordList = FileHelper.ReadTextAllLine(FileHelper.SelectFile());
           
      
            AddToListView(listView1, WordList);
        }


        private void AddToListView(ListView listViewTemp, List<string> list)
        {
            listViewTemp.BeginUpdate();
               list = TagHelper.HandleTag(list);
         
            int allCount = list.Count;
            int complted = 0;
            foreach (string line in list)
            {
                Console.WriteLine("当前{0}%已加载，总计{1}个",(float)complted++/allCount*100,allCount);
              
                if (StaticValue.LockTagList.Contains(line)) {
                    Console.WriteLine("记录过的空指针，跳过");
                    continue;
                }
                int count = GetTagCount.GetCount(line);
                if (count < 10 &&!TextHelper.PureDigits(line))
                {
                
                    FileHelper.AppendUTF8Text(StaticValue.LockTag, line);
                    StaticValue.LockTagList.Add(line);
                    continue;
                }
                string Searchline = TagHelper.SearchCnTag(line);
                string TempLine = "";
                if (Searchline != "")
                    TempLine = line + "|已汉化：" + Searchline;
                else
                    TempLine = line;
                List<string> Tagtemp = new List<string>
                {
                    TempLine,
                    count + ""
                };

                if (Tagtemp.Count == 2) {
                    listViewTemp.Items.Add(ListViewHelper.ListView(Tagtemp));
                }
            }
            listViewTemp.EndUpdate();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(".\\");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            XmlSave();
        }

        private void XmlSave()
        {
            if (textBox3.Text != "" && textBox2.Text != "")
            {
                String JpTAG = textBox2.Text.Trim();
                String Cntag = textBox3.Text.Trim();
                TagHelper.XmlAdd(StaticValue.XmlPath, JpTAG, Cntag);
                textBox2.Text = "";
                textBox3.Text = "";
                TagStruct tagStruct = new TagStruct();
                tagStruct.JpTag1 = JpTAG;
                tagStruct.CnTag1 = Cntag;
                StaticValue.TagStruct.Add(tagStruct);
            }
            else
            {

                MessageBox.Show("汉化标签不能为空");
            }
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                XmlSave();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ExcludeSava();
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ExcludeSava();
            }
        }

        private void ExcludeSava()
        {
            StaticValue.ExcludeList.Add(textBox4.Text.Trim());
            FileHelper.AppendUTF8Text(StaticValue.ExcludePath,textBox4.Text.Trim());
            textBox4.Text = "";
        }

        int Timetext = 0;
        private void button6_Click(object sender, EventArgs e)
        {
            Console.WriteLine(TagHelper.TagShort("Pixiv Id 2697420"));
            ///   Console.WriteLine(HtmlHelper.CodingToUTF8("ラブライブ!サンシャイン!!"));
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
        
        }
    }
}
